package com.yaoxiaowen.project2.test.database.test_realm;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.yaoxiaowen.project2.util.log.debug.LogInterface;
import com.yaoxiaowen.project2.util.log.debug.LogTool;

import  com.yaoxiaowen.project2.test.database.test_xutils_3.DBOpenHelper;

import java.util.List;

import io.realm.Realm;
import io.realm.RealmQuery;
import io.realm.RealmResults;
import io.realm.internal.async.QueryUpdateTask;

/**
 * @author: huangyue on 2016/12/29 11:28.
 * @email: hy42744@ly.com
 *
 * YaoWen(43194) modify  at 2017/2/13 14:40
 * 之前的时候，黄月写了 com.tc.tickets.train.database.DBOpenHelper.java 使用了 sqlite数据库来存储数据。
 * 而后来 ，姚稳将数据库都修改为了 使用 realm,所以在该类中，姚稳也修改为使用realm
 *
 */


public class DBService {

    
    public static final String TAG = "DBService, 数据库";
    private static final LogInterface mLog = LogTool.getLogType();
    private static final boolean DEBUG = true;
    public static final String DOWN_URL_KEY = "downloadUrl";

    public DBService(Context context) {
    }

    /**
     * 保存或更新下载的断点信息
     * 1,如果之前不存在该url，则保存该条信息
     * 2,如果之前已经存在该条rul，则更新该url
     */
    public synchronized void saveMark(String downloadUrl, long mark, float percent) {
        DownloadMarkBean markBean = new DownloadMarkBean(downloadUrl, mark, percent);
        DbBaseHelper dbHelper = new DbBaseHelper();


        //之前不存在这个键
        List<DownloadMarkBean> list = dbHelper.findAll(DownloadMarkBean.class);

        if (list==null || list.size() ==0){
            dbHelper.saveData(markBean);
            dbHelper.close();
            return;
        }
        Realm realm = dbHelper.getRealm();

        RealmQuery<DownloadMarkBean> realmQuery = realm.where(DownloadMarkBean.class);
        DownloadMarkBean markBeanReamObj = null;


        if (realmQuery != null){
            realmQuery = realmQuery.equalTo(DOWN_URL_KEY, downloadUrl);
            if (realmQuery != null){
                markBeanReamObj = realmQuery.findFirst();
            }
        }


        realm.beginTransaction();
        if (markBeanReamObj == null){ //之前不存在
            realm.copyToRealm(markBean);
        }else {
            markBeanReamObj.setDownloadMark(mark);
            markBeanReamObj.setDownloadPercent(percent);
        }
        realm.commitTransaction();
        dbHelper.close();
    }

    /**
     * 获取下载的断点信息
     */
    @SuppressLint("UseSparseArrays")
    public synchronized long getMark(String downloadUrl) {
        long mark = 0;
        Realm realm = Realm.getDefaultInstance();
        DownloadMarkBean markBeanReamObj = realm.where(DownloadMarkBean.class)
                .equalTo(DOWN_URL_KEY, downloadUrl).findFirst();
        if (markBeanReamObj != null){
            mark = markBeanReamObj.getDownloadMark();
        }
        realm.close();
        return mark;
    }

    /**
     * 获得下载进度
     */
    @SuppressLint("UseSparseArrays")
    public synchronized float getPercent(String downloadUrl) {

        DbBaseHelper dbHelper = new DbBaseHelper();

        List<DownloadMarkBean> list = dbHelper.findAll(DownloadMarkBean.class);

        if (list==null || list.size() ==0){
            dbHelper.close();
            return 0.0f;
        }

        float percent = 0;
        Realm realm = dbHelper.getRealm();
        DownloadMarkBean markBeanReamObj = realm.where(DownloadMarkBean.class)
                .equalTo(DOWN_URL_KEY, downloadUrl).findFirst();
        if (markBeanReamObj != null){
            percent = markBeanReamObj.getDownloadPercent();
        }
        dbHelper.close();
        return percent;
    }

    /**
     * 删除下载的断点信息
     */
    public synchronized void deleteLoad(String downloadUrl) {

        DbBaseHelper dbHelper = new DbBaseHelper();
        Realm realm = dbHelper.getRealm();
        RealmResults<DownloadMarkBean> result = realm.where(DownloadMarkBean.class)
                .equalTo(DOWN_URL_KEY, downloadUrl).findAll();
        realm.beginTransaction();
        if (result != null){
            result.deleteAllFromRealm();
        }
        realm.commitTransaction();
        dbHelper.close();

    }


}